module.exports = function($scope, $resource, $modalInstance, obj, toaster){

    $scope.company_name = obj.company_name;
    //本次补贴额
    $scope.actual_money = obj.actual_money;
    
    $scope.countNum = function(){
		$resource('/api/as/puc/subsidycount/getCountForBank', {}, {}).save({'company_code': obj.company_code}, function(res){
            if(res.errcode === 0){
                //可用补贴额
                $scope.remainmoney = res.data.uplimit_money - res.data.issued_money - res.data.no_send_money;
            } else {
                toaster.error({title: '', body: '统计失败'});
            }
        });
	}
    $scope.countNum();
    
    //查询已申请补贴列表
    $scope.load = function(){
        var para = {
            'company_code': obj.company_code,
            'time': obj.time
        };
        $resource('/api/as/puc/subsidycount/findUsedInfoList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
				$scope.list = res.data;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};
    $scope.load();
    
    //审批flag=1通过，flag=-1驳回
    $scope.gogo = function(obj, flag){
        obj.status = flag;
        $scope.actual_money += flag * obj.subsidy_num;
    }

    $scope.pass = function(){
        if(confirm('审批通过后，当前批次内通过的游客才会计算补贴金额！')){
            var para = {
                'id': obj.id,
                'company_code': obj.company_code,
                'time': obj.time,
                'rejectlist': [],
                'money': 0
            }
            angular.forEach($scope.list, function(item){
                if(item.status == '-1'){
                    para.rejectlist.push(item.id);
                } else {
                    para.money += item.subsidy_num;
                }
            });
            if($scope.remainmoney < para.money){
                toaster.warning({title: '', body: '企业剩余补贴额不足，无法通过'});
                return;
            }
            $resource('/api/ac/puc/subsidyCountService/setPass', {}, {}).save(para, function(res){
                if(res.errcode === 0){
                    toaster.success({title: '', body: '成功'});
                    $modalInstance.close();
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }
    }

    $scope.reject = function(){
        if(confirm('审批驳回后，当前批次的游客补贴申请都会被驳回！')){
            $resource('/api/ac/puc/subsidyCountService/setReject', {}, {}).save({'id': obj.id}, function(res){
                if(res.errcode === 0){
                    toaster.success({title: '', body: '成功'});
                    $modalInstance.close();
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }
    }

	$scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
	
};